<div class="container">

    <div class="page-header">
        <div class="row">
            <h1 class="span8"><? if($post_action=='EDIT'){ echo 'Edit';}else echo 'Add';?> <?=ucfirst($post_type)?></h1>
            <span class="span4">

            </span>
        </div>


    </div>

    <div class="row">

        <div class="span8">

            <form class="form-horizontal-qtrans" id="post" method="post" action="javascript:" name="post">

                <input type="hidden" value="<?=$post_type;?>" name="post_type" id="post_type">
                <input type="hidden" value="<?=$post_action;?>" name="post_action" id="post_action">

                <?        
                    if($post_action=='EDIT'){?>
                    <input type="hidden" value="<?=$post['ID'];?>" name="post_ID" id="post_ID">
                    <?}
                ?>

                <fieldset>


                    <div id="post-body">

                        <div id="post-body-content">

                            <!--THIS CONTENT WILL BE HIDDEN BY JAVASCRIPT LANGUAGE-->
                            <div id="titlediv">

                                <div id="titlewrap">

                                    <input type="text" autocomplete="off" id="title" value="<?if($post_action=='EDIT')echo $post['post_title'];?>" tabindex="1" size="30" name="post_title">

                                </div>

                                <div class="inside"></div>

                            </div>
                            <!--/THIS CONTENT WILL BE HIDDEN BY JAVASCRIPT LANGUAGE-->

                            <div class="postarea" id="postdivrich">

                                <div class="wp-editor-wrap tmce-active" id="wp-content-wrap">

                                    <div class="wp-editor-tools" id="wp-content-editor-tools">

                                    </div>

                                    <div id="wp-content-editor-container" class="wp-editor-container">

                                        <!--THIS CONTENT WILL BE HIDDEN BY JAVASCRIPT LANGUAGE-->
                                        <textarea class="wp-editor-area" rows="20" tabindex="1" cols="40" name="content" id="content">
                                            <?if($post_action=='EDIT')echo $post['content'];?>
                                        </textarea>
                                        <!--/THIS CONTENT WILL BE HIDDEN BY JAVASCRIPT LANGUAGE-->

                                        <textarea id="qtrans_textarea_content" name="qtrans_textarea_content" tabindex="2" rows="20" cols="40" style="display:none" onblur="qtrans_save(this.value);"></textarea>

                                    </div>

                                </div>

                            </div>

                        </div>

                    </div>
                    
                    <div class="parent well" style="margin-top: 23px; margin-bottom: 23px;">
                        <h3 style="padding-left: 0;"><?=ucfirst($post_type)?> properties</h3>
                        <label for="parent_id" class="screen-reader-text">Parent</label>
                        <select id="parent_id" name="parent_id">
                            <option value="0" selected="selected">(no parent)</option>
                            <?  
                                $selected = '';
                                foreach($all_posts as $one_post): ?>
                                <? if($one_post['ID']!=$post['ID']){ ?>
                                    <? if($post_action=='EDIT'){                                
                                            if($one_post['ID']==$post['parent_ID']){
                                                $selected = 'selected="selected"';
                                            }  
                                        }

                                    ?>
                                    <option value="<?=$one_post['ID']?>" class="level-0" <?echo $selected;?>><?=$one_post['name']?></option>
                                    <?}?>
                                <? 
                                    $selected='';
                                    endforeach; ?>
                        </select>
                    </div>

                    <div class="form-actions">

                        <div class="row">
                            <span class="span2 save"><button class="btn btn-primary" type="submit">Save changes</button></span>
                            <span class="span6 loader"><img id="ajax-loading" class="ajax-loading" src="<?=base_url()?>assets/img/backgnds/loading.gif" style="visibility: hidden;"></span>
                        </div>

                    </div>

                </fieldset>
            </form>

        </div>

        <div class="span4">

            <div class="hero-unit">
                <h3><?=ucfirst($post_type)?></h3>
                <p>
                    <a class="btn btn-primary btn-large" href="<?=base_url('posts/view_all?post_type='.$post_type)?>">
                        View All <?=ucfirst($post_type)?>
                    </a>
                </p>
            </div>

        </div>
    </div>

</div> <!-- /container -->

<!--INPUT TITLE & TINY MCE QTRANS PROCEESSING--> 
<script type="text/javascript">
    var wp_languages = new Array();
    var wp_languages_short = new Array();
    <?

        $i=0;
        foreach($wp_languages as $key=>$value){

        ?>

        wp_languages['<?=$i?>'] = '<?=$value?>';
        wp_languages_short['<?=$i?>'] = '<?=$key?>';

        <?
            $i++;
        }
    ?>
</script>

<script src="<?=base_url()?>assets/js/qtrans/title.js" type="text/javascript"></script>

<script type="text/javascript">

    jQuery(document).ready(function() {

            qtrans_editorInit1();

            // Create elements
            jQuery.each(wp_languages_short, function(key, value) {

                    //console.log('key:'+key+' -> value: '+value);

                    var td = document.getElementById('titlediv');
                    var qtd = document.createElement('div');
                    var h = document.createElement('h3');
                    var l = document.createTextNode('Title ('+wp_languages[key]+')');
                    var tw = document.createElement('div');
                    var ti = document.createElement('input');
                    var slug = document.getElementById('edit-slug-box');

                    ti.type = 'text';
                    ti.id = 'qtrans_title_'+value;
                    ti.tabIndex = '1';
                    ti.value = qtrans_use(value, document.getElementById('title').value);
                    ti.onchange = qtrans_integrate_title;
                    ti.className = 'qtrans_title_input';
                    h.className = 'qtrans_title';
                    tw.className = 'qtrans_title_wrap';

                    qtd.className = 'postarea';

                    h.appendChild(l);
                    tw.appendChild(ti);
                    qtd.appendChild(h);
                    qtd.appendChild(tw);
                    td.parentNode.insertBefore(qtd,td);

            });                                             

            document.getElementById('titlediv').style.display='none'; 

    });



</script>

<script src="<?=base_url()?>assets/js/qtrans/tinymce.js" type="text/javascript"></script>
<!--/INPUT TITLE & TINY MCE QTRANS PROCEESSING-->

<!--POST DELETE HANDLER--> 
<script type="text/javascript">
    jQuery(document).ready(function() {

            jQuery('.delete_post').live('click',function(){

                    var conf = confirm("Delete <?=ucfirst($post_type)?>?")
                    if (!conf){
                        return false;
                    }

                    window.location.href = jQuery(this).attr('href');

                    return false;

            });

    } );
</script>
<!--/POST DELETE HANDLER-->

<!--FORM SUBMIT HANDLER-->
<script>

    jQuery(document).ready(function() {       

            // Override default error message
            jQuery.validator.messages.required = "";

            // Override generation of error label
            $("#post").validate({
                    submitHandler: function(form) {

                        $('input[type=submit]', '#post').attr('disabled', 'disabled');

                        qtrans_save(tinyMCE.get('qtrans_textarea_content').getContent()); 
                        $('#ajax-loading').css('visibility','visible');

                        var form = $("#post");
                        var form_data = form.serialize();

                        $.ajax({
                                url: base_url+'posts/submit_post',
                                dataType: 'json',
                                type: "POST",
                                data: form_data,
                                success: function (data, textStatus, xhr) {
                                    jQuery('#ajax-loading').css('visibility','hidden');

                                    if(data.action==true){

                                        jQuery('.loader').html(data.msg);

                                        // REDIRECT
                                        function promo_show(){
                                            window.location.href = base_url+'posts/view_all?post_type=<?=$post_type?>'; 
                                        }
                                        window.setTimeout(function() { promo_show(); }, 1001);

                                    }else{

                                        $('#post').removeAttr('disabled');                             
                                        alert(data.msg)  
                                    }
                                },
                                error: function (xhr, textStatus, errorThrown) {
                                    alert('Error ocured...\n Server not found.');
                                    $('input[type=submit]', '#post').removeAttr('disabled');
                                    $('#ajax-loading').css('visibility','hidden');
                                }
                        });

                    },
                    errorPlacement: function(error, element){}
            });

            $('input[type=submit]', '#post').removeAttr('disabled'); 


    });

</script>
<!--/FORM SUBMIT HANDLER--> 